
# __VIEWSTATE: +P7KbBlUMlXPWRuL18Fn3wVuUh4kmX/NdaDy4sJjrbg/+4EGzDq6V5YzX9IXzN72kAaqehcmJgppWfr6Sav0nhFHCBCwsQACxWxzrFdJe1Bjpl3Or+ddKgR9jE0TeyD1uNxztUta9HVFbpGUbPBuaLvKzbo=
# __VIEWSTATEGENERATOR: C93BE1AE
# from: http://www.gushiwen.cn/user/collect.aspx?sort=t
# email: cxlpkxp@sina.com
# pwd: asdasss
# code: IX73
# denglu: 登录
# pkzwy#
import requests

url = 'https://www.gushiwen.cn/user/login.aspx?from=http://www.gushiwen.cn/user/collect.aspx'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
}

response = requests.get(url, headers=headers)

content = response.text

from bs4 import BeautifulSoup

soup = BeautifulSoup(content, 'lxml')
viewstate = soup.select('#__VIEWSTATE')[0].attrs.get("value")

viewstategenerator = soup.select('#__VIEWSTATEGENERATOR')[0].attrs.get("value")

# print(viewstate)
# print(viewstategenerator)

# 获取验证码图片
code = soup.select("#imgCode")[0].attrs.get("src")
code_url = "https://www.gushiwen.cn" + code
# import urllib.request
# urllib.request.urlretrieve(url=code_url, filename='code.png')

session = requests.session()

response_code = session.get(code_url)
content_code = response_code.content
# wb的模式就是将二进制数据写入到文件
with open('code.png', 'wb') as f:
    f.write(content_code)

code_name = input("请输入你的验证码")

url_post = "https://www.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fwww.gushiwen.cn%2fuser%2fcollect.aspx"
data_post = {
    '__VIEWSTATE': viewstate,
    '__VIEWSTATEGENERATOR': viewstategenerator,
    'from': 'http://www.gushiwen.cn/user/collect.aspx',
    'email': 'cxlpkxp@sina.com',
    'pwd': 'cxlpkzwy#',
    'code': code_name,
    'denglu': '登录'
}

response_post = session.post(url=url_post, data=data_post,headers=headers)

content_post = response_post.text

with open('gushiwen.html','w',encoding='utf-8') as f:
    f.write(content_post)

# 难点
# 隐藏域
# 验证码